using Godot;
using System;
using System.Collections.Generic;
using Common;
using Library;

namespace DRE.Battle {
	public class PlayerPanel : Control {
		bool activated;
		public bool Activated {
			get => activated;
			set {
				activated = value;
				slidingBorder.Visible = activated;
				buttonsBorder.Visible = activated;
				buttonAnimations.Visible=activated;
				if (activated) {
					StartSliding(Easing.CircOut, -34, 0.3f);
				} else {
					StartSliding(Easing.SineOut, 0, 0.07f);
				}
				UpdateButtons();
			}
		}
		Color color;
		public Color Color {
			get => color;
			set {
				color = value;
				slidingBorder.Modulate = color;
				buttonsBorder.Modulate = color;
				hpBar.Color = color;
				buttonAnimations.Modulate=color;
			}
		}
		string name = "";
		public new string Name {
			get => name;
			set {
				name = value;
			}
		}
		float maxHp;
		public float MaxHp {
			get => maxHp;
			set {
				maxHp = value;
				maxHpLabel.Text = maxHp.ToString();
				UpdateHpBar();
			}
		}
		float hp;
		public float Hp {
			get => hp;
			set {
				hp = value;
				hpLabel.Text = hp.ToString();
				UpdateHpBar();
			}
		}
		public int selectedButton = -1;
		public int SelectedButton {
			get => selectedButton;
			set {
				selectedButton = value;
				UpdateButtons();
			}
		}

		void UpdateHpBar() {
			hpBar.RectSize = new Vector2(Mathf.Round(hpBarBg.RectSize.x / MaxHp * Hp), hpBarBg.RectSize.y);
		}
		void UpdateButtons() {
			for (int i = 0; i < buttonSprites.Count; i += 1) {
				buttonSprites[i].Index = (Activated && selectedButton > 0 && selectedButton == i ? 1 : 0);
			}
		}

		NinePatchRect slidingBorder;
		NinePatchRect buttonsBorder;
		ColorRect hpBarBg;
		ColorRect hpBar;
		Label hpLabel;
		Label maxHpLabel;
		Control sliding;
		Control buttonAnimations;

		bool slidingAnim;
		float slidingFrom;
		float slidingTo;
		float slidingDuration;
		float slidingTime;
		Easing.Delegate slidingEasing;

		List<SpriteController> buttonSprites = new List<SpriteController>();


		public override void _Ready() {
			sliding = GetNode<Control>("Sliding/Panel");
			slidingBorder = GetNode<NinePatchRect>("Sliding/Panel/Border");
			buttonsBorder = GetNode<NinePatchRect>("Buttons/Border");
			hpBarBg = GetNode<ColorRect>("Sliding/Panel/Content/HpBar");
			hpBar = GetNode<ColorRect>("Sliding/Panel/Content/HpBar/Value");
			hpLabel = GetNode<Label>("Sliding/Panel/Content/CurrentHp");
			maxHpLabel = GetNode<Label>("Sliding/Panel/Content/MaxHp");
			buttonAnimations=GetNode<Control>("Buttons/Content/Animations");

			foreach (SpriteController node in GetNode<Control>("Buttons/Content/Buttons").GetChildren()) {
				buttonSprites.Add(node);
			}

			Activated = false;
			Color = new Color(0, 1, 1);
			MaxHp = 514;
			Hp = 114;
		}

		void StartSliding(Easing.Delegate easing, float to, float duration) {
			slidingAnim = true;
			slidingFrom = sliding.RectPosition.y;
			slidingTo = to;
			slidingDuration = duration;
			slidingTime = 0;
			slidingEasing = easing;
		}

		public override void _Process(float delta) {
			if (Input.IsActionJustPressed("Confirm")) {
				Activated = !Activated;
			}
			if (slidingAnim) {
				slidingTime += delta;
				if (slidingTime >= slidingDuration) {
					slidingTime = slidingDuration;
					slidingAnim = false;
				}
				//GD.Print(string.Format("Easing: From[{0}] To[{1}] Dur[{2}] Time[{3}] Value[{4}]",slidingFrom,slidingTo,slidingDuration,slidingTime,sliding.RectPosition.y));
				sliding.RectPosition = new Vector2(0, slidingEasing.Invoke(slidingTime, slidingDuration, slidingFrom, slidingTo));
			}
		}
	}
}
